//
//  ESTActivityDetailViewController.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/10/16.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTActivityDetailViewController.h"
#import "ESTActivityModel.h"
#import "ESTPickerTipView.h"
#import "ESTAcitvityDetailViewCell1.h"
#import "ESTAcitvityDetailViewCell2.h"
#import "UIButton+WebCache.h"
#import "TYZBigImageView.h"
#import "ESTFollowModeViewController.h"

@interface ESTActivityDetailViewController ()<UITableViewDelegate, UITableViewDataSource>
/**
 *  TableView
 */
@property(nonatomic, strong) TYZTableView *tableView;
/**
 *  所有活动模型
 */
@property(nonatomic, strong) NSArray<ESTActivityModel *> *modelArray;
/**
 *  第几个活动
 */
@property(nonatomic, assign) NSInteger number;
/**
 *  右上角选择按钮
 */
@property(nonatomic, strong) ESTPickerTipView *activityPickerView;
@end

@implementation ESTActivityDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.mainTitle = self.modelArray[self.number].ACTIVITY_NAME;
    self.view.backgroundColor = TYZWhiteColor;
    [self setupRightUpButton];
    [self setupBottomButton];
    [self setupTableView];
    [self acquireActivity];
}
- (void)acquireActivity {
    WEAKSELF
    [TYZLoadingView showMessage:nil];
    [ESTActivityModel requestAllActivitySuccess:^(NSArray<ESTActivityModel *> *modelArray) {
        weakSelf.modelArray = modelArray;
        NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:modelArray.count];
        [modelArray enumerateObjectsUsingBlock:^(ESTActivityModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [arrM addObject:obj.ACTIVITY_NAME];
        }];
        weakSelf.activityPickerView.dataArray = [arrM mutableCopy];
        [TYZLoadingView hideHUD];
    } failure:^(NSString *error) {
        weakSelf.modelArray = nil;
        [TYZLoadingView hideHUD];
    }];
}
#pragma mark - 设置UI
// 其它活动
- (void)setupRightUpButton {
    ESTPickerTipView *activityPickerView = [[ESTPickerTipView alloc] init];
    activityPickerView.title = [@"其它活动" S];
    activityPickerView.textColor = PickerTipViewTextColor_White;
    activityPickerView.titleLabel.font = [UIFont systemFontOfSize:CoreInfo.fontSize];
    activityPickerView.pickerTitle = [@"选择活动" S];
    self.activityPickerView = activityPickerView;
    WEAKSELF
    __weak typeof(activityPickerView) weakPickerView = activityPickerView;
    [activityPickerView setDidChooseNumberBlock:^(NSArray<NSNumber *> *numberArray) {
        weakSelf.number = [numberArray.lastObject integerValue];
        weakPickerView.selectedValue = @[weakSelf.mainTitle];
    }];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:activityPickerView];
    self.navigationItem.rightBarButtonItem = rightItem;
}
- (void)setupBottomButton {
    TYZButton *inviteButton = [TYZButton buttonWithType:UIButtonTypeCustom];
    NSString *title = self.isSpaekDetail ? [@"我已经了解完了话术，立即邀约" S] : [@"立即邀约" S];
    [self.view addSubview:inviteButton];
    [inviteButton setTitle:title forState:UIControlStateNormal];
    [inviteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.mas_equalTo(50);
    }];
    [inviteButton setBackgroundColor:ThemeColor];
    [inviteButton addTarget:self action:@selector(didClickInviteButton) forControlEvents:UIControlEventTouchUpInside];
}
- (void)setupTableView {
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-50);
    }];
    self.tableView.estimatedRowHeight = 100;
}
#pragma mark - actions
- (void)didClickInviteButton {
    ESTFollowModeViewController *controller = [[ESTFollowModeViewController alloc] init];
    controller.activity_id = self.activity_id;
    controller.mode = FollowUpMode_First;
    [self.navigationController pushViewController:controller animated:YES];
}
- (void)didClickImageButton {
    [TYZBigImageView bigImageWithURLString:self.modelArray[self.number].ACTIVITY_ATTA_URL1];
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.isSpaekDetail) {
        return 1;
    }
    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isSpaekDetail) {
        return UITableViewAutomaticDimension;
    }
    if (indexPath.row == 0) {
        return 130;
    } else if (indexPath.row == 2) {
        return (kWindowWidth - 20) * 0.75;
    }
    return UITableViewAutomaticDimension;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isSpaekDetail) {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ESTActivityDetailViewController"];
        UILabel *speakLable = [[UILabel alloc] init];
        [cell.contentView addSubview:speakLable];
        [speakLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(cell.contentView);
        }];
        speakLable.numberOfLines = 0;
        speakLable.text = self.modelArray[self.number].ACTIVITY_FOLLOW_DESC;
        speakLable.font = [UIFont systemFontOfSize:CoreInfo.fontSize];
        speakLable.textColor = TYZBlackColor;
        speakLable.backgroundColor = TYZClearColor;
        return cell;
    }
    if (indexPath.row == 0) {
        ESTAcitvityDetailViewCell1 *cell = [ESTAcitvityDetailViewCell1 creatAcitvityDetailViewCell1:tableView];
        cell.model = self.modelArray[self.number];
        WEAKSELF
        [cell setDidClickSpeakBlock:^{
            ESTActivityDetailViewController *controller = [[ESTActivityDetailViewController alloc] init];
            controller.modelArray = weakSelf.modelArray;
            controller.number = weakSelf.number;
            controller.isSpaekDetail = YES;
            [weakSelf.navigationController pushViewController:controller animated:YES];
        }];
        return cell;
    } else if (indexPath.row == 1) {
        ESTAcitvityDetailViewCell2 *cell = [ESTAcitvityDetailViewCell2 creatAcitvityDetailViewCell2:tableView title:[@"活动主题" S] content:self.modelArray[self.number].ACTIVITY_THEME];
        return cell;
    } else if (indexPath.row == 3) {
        ESTAcitvityDetailViewCell2 *cell = [ESTAcitvityDetailViewCell2 creatAcitvityDetailViewCell2:tableView title:[@"活动内容" S] content:self.modelArray[self.number].ACTIVITY_CONTENT];
        return cell;
    }
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ESTActivityDetailViewController"];
    TYZButton *button = [[TYZButton alloc] init];
    [cell.contentView addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.contentView).offset(10);
        make.right.equalTo(cell.contentView).offset(-10);
        make.top.bottom.equalTo(cell.contentView);
    }];
    [button sd_setImageWithURL:[NSURL URLWithString:self.modelArray[self.number].ACTIVITY_ATTA_URL1] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:Image_PlaceholderImage_Normal]];
    [button addTarget:self action:@selector(didClickImageButton) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}
#pragma mark - setter & getter
- (void)setNumber:(NSInteger)number {
    _number = number;
    self.mainTitle = self.modelArray[self.number].ACTIVITY_NAME;
    self.activityPickerView.selectedValue = @[self.mainTitle];
}
- (TYZTableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[TYZTableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = TYZWhiteColor;
        _tableView.bounces = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
- (void)setModelArray:(NSArray<ESTActivityModel *> *)modelArray {
    _modelArray = modelArray;
    [modelArray enumerateObjectsUsingBlock:^(ESTActivityModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.ACTIVITY_ID isEqualToString:self.activity_id]) {
            self.number = idx;
            *stop = YES;
        }
    }];
    if (modelArray.count > 0) {
        [self.tableView removeNoDataImage];
        [self.tableView reloadData];
    } else {
        WEAKSELF
        [self.tableView noDataWithClick:^{
            [weakSelf acquireActivity];
        }];
    }
}
@end
